{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    NSApplicationBundleIdentifier = \"com.microsoft.VSCode\";\n",
      "    NSApplicationName = Code;\n",
      "    NSApplicationPath = \"/Applications/Visual Studio Code.app\";\n",
      "    NSApplicationProcessIdentifier = 2275;\n",
      "    NSApplicationProcessSerialNumberHigh = 0;\n",
      "    NSApplicationProcessSerialNumberLow = 450670;\n",
      "    NSWorkspaceApplicationKey = \"<NSRunningApplication: 0x11584dc00 (com.microsoft.VSCode - 2275) LSASN:{hi=0x0;lo=0x6e06e}>\";\n",
      "}\n",
      "psutil.Process(pid=2275, name='Electron', status='running', started='17:52:37')\n"
     ]
    }
   ],
   "source": [
    "from AppKit import NSWorkspace, NSProcessInfo\n",
    "app = NSWorkspace.sharedWorkspace().activeApplication()\n",
    "# appName = NSProcessInfo.processInfo()\n",
    "\n",
    "import psutil\n",
    "p = psutil.Process(app['NSApplicationProcessIdentifier'])\n",
    "\n",
    "print(app)\n",
    "print(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Application name: ['Electron']\n",
      "Window title: ['mac.ipynb — timelog']\n"
     ]
    }
   ],
   "source": [
    "import appscript\n",
    "\n",
    "# 获取前置应用程序和窗口\n",
    "front_app = appscript.app('System Events').application_processes[appscript.its.frontmost == True].name.get()\n",
    "front_win = appscript.app('System Events').application_processes[appscript.its.frontmost == True].windows[1].name.get()\n",
    "\n",
    "print(f\"Application name: {front_app}\")\n",
    "print(f\"Window title: {front_win}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import ImageGrab\n",
    "from datetime import datetime\n",
    "import time\n",
    "\n",
    "scale = 0.33\n",
    "now = datetime.now()\n",
    "img = ImageGrab.grab()\n",
    "img = img.resize((int(img.width * scale), int(img.height * scale)))\n",
    "img.save('./test.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['https://search.bilibili.com/all?keyword=mac+%E7%BC%96%E7%A8%8B&page=3',\n",
       "  'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=68018901_17_oem_dg&wd=mac%20%E8%8E%B7%E5%8F%96chrome%E7%9A%84%E6%A0%87%E9%A2%98&oq=python%2520dir%2520%25E6%2598%25BE%25E7%25A4%25BA%25E4%25B8%258D%25E5%25AE%258C%25E6%2595%25B4&rsv_pq=8831d35c00012f17&rsv_t=38d8cxJgybIntJSoNI6ToHcTeNRafE4XuD2xGCmiJNKrzvzTSNtCntIkrTmsKtBcZ2xo5d6nVl5S&rqlang=cn&rsv_dl=tb&rsv_enter=1&rsv_btype=t&inputT=8258&rsv_sug3=59&rsv_sug1=33&rsv_sug7=100&rsv_sug2=0&rsv_sug4=8259',\n",
       "  'https://www.cnpython.com/qa/319331',\n",
       "  'https://www.shuzhiduo.com/topic/%E4%BD%BF%E7%94%A8c-%E8%8E%B7%E5%8F%96chrome%E6%B5%8F%E8%A7%88%E5%99%A8%E6%A0%87%E9%A2%98/']]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import appscript\n",
    "urls = appscript.app('Microsoft Edge').windows.tabs.URL()\n",
    "urls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['python appscript - Google 搜索'] ['https://www.google.com/search?q=python+appscript&ei=9OHlY-mnG6qRur8PkJGymAE&start=20&sa=N&ved=2ahUKEwjp8bDYp4r9AhWqiO4BHZCIDBM4ChDw0wN6BAgCEBc']\n"
     ]
    }
   ],
   "source": [
    "app = appscript.app('Microsoft Edge')\n",
    "print(\n",
    "    app.windows.active_tab.title(),\n",
    "    app.windows.active_tab.URL(),\n",
    "    app.windows.active_tab.URL(),\n",
    "      )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "Use class methods to instantiate new Objective-C objects",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m/Users/fanque/workspace/timelog/mac.ipynb 单元格 5\u001b[0m in \u001b[0;36m<cell line: 4>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/fanque/workspace/timelog/mac.ipynb#W4sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mNotificationCenter\u001b[39;00m\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/fanque/workspace/timelog/mac.ipynb#W4sZmlsZQ%3D%3D?line=2'>3</a>\u001b[0m \u001b[39m# set up the notification\u001b[39;00m\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/fanque/workspace/timelog/mac.ipynb#W4sZmlsZQ%3D%3D?line=3'>4</a>\u001b[0m notification \u001b[39m=\u001b[39m NotificationCenter\u001b[39m.\u001b[39;49mNSUserNotification()\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/fanque/workspace/timelog/mac.ipynb#W4sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m notification\u001b[39m.\u001b[39mtitle \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39mNew Notification\u001b[39m\u001b[39m'\u001b[39m\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/fanque/workspace/timelog/mac.ipynb#W4sZmlsZQ%3D%3D?line=5'>6</a>\u001b[0m notification\u001b[39m.\u001b[39msubtitle \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39mThis is a new notification\u001b[39m\u001b[39m'\u001b[39m\n",
      "\u001b[0;31mTypeError\u001b[0m: Use class methods to instantiate new Objective-C objects"
     ]
    }
   ],
   "source": [
    "import NotificationCenter\n",
    "\n",
    "# set up the notification\n",
    "notification = NotificationCenter.NSUserNotification()\n",
    "notification.title = 'New Notification'\n",
    "notification.subtitle = 'This is a new notification'\n",
    "notification.informativeText = 'This is the informative text of the notification'\n",
    "\n",
    "# deliver the notification\n",
    "center = NotificationCenter.NSUserNotificationCenter.defaultUserNotificationCenter()\n",
    "center.deliverNotification_(notification)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "118:130: execution error: 变量“notification”没有定义。 (-2753)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "256"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "# 要显示的通知内容\n",
    "title = \"通知标题\"\n",
    "subtitle = \"通知子标题\"\n",
    "message = \"这是通知的主要消息内容\"\n",
    "\n",
    "# 使用AppleScript创建和显示通知\n",
    "command = f\"\"\"\n",
    "    display notification \"{message}\" with title \"{title}\" subtitle \"{subtitle}\" sound name \"default\"\n",
    "    delay 5\n",
    "    close notification\n",
    "\"\"\"\n",
    "os.system(f\"osascript -e '{command}'\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "05d23446d46242dda2c401b0d9324060e9adb6825ff8855a376f7edd189616b7"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
